import http from "../../../../../utils/http"
import { RES_HOST } from "../../../../../utils/const"

Component({
  properties: {
    // 这里定义了innerText属性，属性值可以在组件使用时指定
    taskList: {
      type: Boolean,
      observer: function (oldData, newData) {
        this.setData({
          taskList: oldData
        })
      }
    },
    taskData: {
      type: Array,
      observer: function (oldData, newData) {
        this.setData({
          list: oldData
        })
      }
    }
  },
  data: {
    //删除按钮宽度单位（rpx）
    delBtnWidth: 180,
    list: [],
    //判断是否完成:
    taskList: '',
    res_host: RES_HOST,
    isEvent: true,
    oldItem: ''
  },
  attached: function () {
    this.initEleWidth();
  },
  methods: {
    // 这里是一个自定义方法
    touchS: function (e) {
      if (e.touches.length == 1) {
        this.setData({
          //设置触摸起始点水平方向位置
          startX: e.touches[0].clientX
        });
      }
    },
    touchM: function (e) {
      if (e.touches.length == 1) {
        //手指移动时水平方向位置
        var moveX = e.touches[0].clientX;
        //手指起始点位置与移动期间的差值
        var disX = this.data.startX - moveX;
        var delBtnWidth = this.data.delBtnWidth;
        var txtStyle = "";
        if (disX == 0 || disX < 0) {//如果移动距离小于等于0，文本层位置不变
          txtStyle = "left:0px";
        } else if (disX > 0) {//移动距离大于0，文本层left值等于手指移动距离
          txtStyle = "left:-" + disX + "px";
          if (disX >= delBtnWidth) {
            //控制手指移动距离最大值为删除按钮的宽度
            txtStyle = "left:-" + delBtnWidth + "px";
          }
        }
        //获取手指触摸的是哪一项
        var index = e.currentTarget.dataset.index;
        var list = this.data.list;
        if (index >= 0) {
          list[index].txtStyle = txtStyle;
          //更新列表的状态
          this.setData({
            list: list
          });
        }
      }
    },
    initEleWidth: function () {
      var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
      this.setData({
        delBtnWidth: delBtnWidth
      });
    },
    //获取元素自适应后的实际宽度
    getEleWidth: function (w) {
      var real = 0;
      try {
        var res = wx.getSystemInfoSync().windowWidth;
        var scale = (750 / 2) / (w / 2);//以宽度750px设计稿做宽度的自适应
        // console.log(scale);
        real = Math.floor(res / scale);
        return real;
      } catch (e) {
        return false;
        // Do something when catch error
      }
    },
    touchE: function (e) {
      let that = this;
      if (e.changedTouches.length == 1) {
        //手指移动结束后水平位置
        var endX = e.changedTouches[0].clientX;
        //触摸开始与结束，手指移动的距离
        var disX = this.data.startX - endX;
        var delBtnWidth = this.data.delBtnWidth;
        //如果距离小于删除按钮的1/2，不显示删除按钮
        var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
        //获取手指触摸的是哪一项
        var index = e.currentTarget.dataset.index;
        var list = this.data.list;
        this.setData({
          oldItem: index
        })
        if (index >= 0) {
          list[index].txtStyle = txtStyle;
          //更新列表的状态
          list.forEach(function (v, index) {
            if (index != that.data.oldItem) {
              v.txtStyle = "left:0px"
            }
          })
          let str = txtStyle.substring(5);
          let reg = /^\-+/g
          reg.lastIndex = 0;
          if (reg.test(str)) {
            this.setData({
              isEvent: false
            })
          } else {
            setTimeout(function () {
              that.setData({
                isEvent: true
              })
            }, 600)

          }
          this.setData({
            list: list
          });
        }
      }
    },
    //点击删除按钮事件
    delItem: function (e) {
      //获取列表中要删除项的下标
      let id = e.currentTarget.dataset.id;
      let list = this.data.list;
      let num = this.delNum(list, id);
      let that = this;
      http({
        url: "/look/deleteReservationList/v1.0.0",
        method: "POST",
        data: {
          id: id
        },
        success: function (res) {
          if (res.code == 200) {
            that.triggerEvent('itemsdel', { id: id });
            list.splice(num, 1);
            that.setData({
              list: list
            })
          }
        }
      })
    },
    //点击进入房源
    detailFang: function (e) {
      let that = this;
      if (this.data.isEvent) {
        if (e.type == 'tap') {
          if (this.data.taskList) {
            let id = e.currentTarget.dataset.id;
            wx.navigateTo({
              url: '../appointmentDetail/appointmentDetail?id=' + id + '&tasklist=' + that.data.taskList
            })
          } else {
            let id = e.currentTarget.dataset.id;
            wx.navigateTo({
              url: '../appointmentDetail/appointmentDetail?id=' + id + '&tasklist=' + that.data.taskList
            })
          }
        } else {
          return false;
        }
      }
    },
    //联系物业
    wuye_fang: function (e) {
      let phone = e.currentTarget.dataset.phone;
      if (this.data.isEvent) {
        wx.makePhoneCall({
          phoneNumber: phone
        });
      }
    },
    //完成看房
    success_fang: function (e) {
      let id = e.currentTarget.dataset.id;
      let that = this;
      if (this.data.isEvent) {
        wx.showModal({
          title: '提示',
          content: '您已经完成看房了吗？',
          confirmText: '完成看房',
          success: function (res) {
            if (res.confirm) {
              that.getSuccess(id);
            } else {
              return false
            }
          }
        });
      }
    },
    //完成看房数据请求
    getSuccess: function (id) {
      let that = this;
      let key = "detailData.finishTime"
      let num = that.delNum(that.data.list, id)
      let list = this.data.list;
      http({
        url: "/look/updateInfo/v1.0.0look",
        method: "POST",
        data: {
          id: id,
          status: 1
        },
        success: function (res) {
          if (res.code == 200) {
            that.triggerEvent('itemssuccess', { id: id });
            list.splice(num, 1);
            that.setData({
              list: list
            })
          } else {
            console.log("约看完成请求出错")
          }
        }
      })
    },
    // 获取选中的ID下标
    delNum: function (data, target) {
      var num = '';
      data.forEach(function (val, index) {
        if (val.id == target) {
          num = index;
        }
      })
      return num;
    }
  }
})